const bcrypt = require('bcrypt')
const User = require('../model/user')
const checkPassword = async(username, password) => {
  const data = await User.findOne({ username })
  return String(data.password) === password
}
const checkUsername = async (username) => {
  const data = await User.findOne({ username })
  if (data) {
    return false
  }
  return true
}

// 查找用户
const findUser = async (username) => {
  return await User.findOne({ username })
}

const saveUser = async(username, password, name, created) => {
  password = await bcrypt.hash(password, 5)
  const user = new User({
    username,
    password,
    name,
    created
  })
  return await user.save()
}
module.exports = {
  checkPassword,
  saveUser,
  checkUsername,
  findUser
}
